<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./css/bootstrap-v4.6.0.css">
  <link rel="stylesheet" href="./css/login.css">
</head>

<body>
  <div class="login-box">
    <div class="form-group">
      <label for="username">Account</label>
      <!-- 账号 -->
      <input type="text" class="form-control" id="username" autocomplete="off">
      <small id="emailHelp" class="form-text text-muted">The available account is <strong>admin</strong></small>
    </div>
    <div class="form-group">
      <!-- 密码 -->
      <label for="password">Password</label>
      <input type="password" class="form-control" id="password">
      <small id="emailHelp" class="form-text text-muted">The available password is <strong>123456</strong></small>
    </div>
    <button type="submit" class="btn btn-primary" id="btnLogin">Submit</button>
  </div>

  <script src="./lib/axios.js"></script>

  <script>
    // 请求方式 POST
    // 地址 http://www.liulongbin.top:3009/api/reg
    // 参数 username 用户名     password  密码


    //封装渲染
    function render() {
      //获取元素
      const username = document.querySelector('#username')
      const password = document.querySelector('#password')
      const btnLogin = document.querySelector('#btnLogin')
      
      //注册提交事件

      btnLogin.addEventListener('click', function () {
        // 创建XMLHTTPRequest对象
        const xhr = new XMLHttpRequest()
        // 指定请求方法和url
        xhr.open('POST', 'http://www.liulongbin.top:3009/api/reg')
          // 设置请求头内容类型为application/json
          xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onreadystatechange = function () {
          if (this.readyState === 4 && this.status === 200) {
            console.log(this.responseText);

          }
        }
       
        // 将表单数据封装成JSON格式字符串
        const data = {
          username: username.value,
          password: password.value
        };
        const jsonData = JSON.stringify(data);
        // 发送POST请求
        xhr.send(jsonData);
console.log(typeof jsonData);//打印查看是否是字符串

      username.value=''
      password.value=''

      })

    
      
    
    }
    render()
  
  </script>

</body>

</html>